package demo.xiaxl.com.android_demo.pagersnaphelper;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;

public class FixRecyclerView extends RecyclerView {

    float lastY = 0;
    Scrollable scrollable = null;

    public FixRecyclerView(Context context) {
        super(context);
    }

    public FixRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FixRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        if(scrollable != null){
            switch (e.getAction()){
                case ACTION_MOVE:
                    System.out.println("ACTION_MOVE, y:" +e.getY());
                    float y = e.getY();
                    if (y > lastY) {
                        //move down
                        System.out.println("move down,canContentScrollDown:" + scrollable.canContentScrollDown());
                        return !scrollable.canContentScrollDown();
                    } else {
                        //move up
                        System.out.println("move down,canContentScrollUp:" + scrollable.canContentScrollUp());
                        return !scrollable.canContentScrollUp();
                    }
                case ACTION_DOWN:
                    System.out.println("ACTION_DOWN, y:" +e.getY());
                    lastY = e.getY();
                    break;
                case ACTION_UP:
                    System.out.println("ACTION_UP, y:" +e.getY());
                    lastY = 0;
                    break;
            }
        }
        return super.onInterceptTouchEvent(e);
    }

    public void setCanScrollListener(Scrollable scrollable) {
        this.scrollable = scrollable;
    }

    interface Scrollable {
        boolean canContentScrollUp();

        boolean canContentScrollDown();
    }
}
